local skel = fk.CreateSkill {
  name = "emo__xiaxing",
  tags = {Skill.Compulsory},
}

skel:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = room:getTag("emo__xiaxing_skills")
    if skills == nil then
      skills = {{}, {}, {}}
      for _, general_name in ipairs(room.general_pile) do
        local general = Fk.generals[general_name]
        for _, skill_name in ipairs(general:getSkillNameList()) do
          local skill = Fk.skills[skill_name]
          local tags = (skill:getSkeleton() or {} ).tags or {}
          if #tags == 0 or (#tags == 1 and tags[1] == Skill.Compulsory) then
            local str = Fk:translate(":"..skill_name):gsub("<[^>]+>", "")
            for i = 1, 3 do
              if #str / 3 >= (i-1) * 30 and #str / 3 < i * 30 then
                table.insertIfNeed(skills[i], skill_name)
              end
            end
          end
        end
      end
      room:setTag("emo__xiaxing_skills", skills)
    end
    local choices = {}
    for _, all in ipairs(skills) do
      local sk = table.filter(all, function (s) return not player:hasSkill(s, true) end)
      if #sk > 0 then
        table.insert(choices, table.random(sk))
      end
    end
    if #choices > 0 then
      local skill = room:askToChoice(player, {choices = choices, skill_name = skel.name, detailed = true, prompt = "#emo__xiaxing-skill"})
      local mark = player:getTableMark("emo__xiaxing_record")
      table.insert(mark, skill)
      room:setPlayerMark(player, "emo__xiaxing_record", mark)
      room:handleAddLoseSkills(player, skill)
    end
  end,
})

skel:addEffect(fk.RoundEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return type(player:getMark("emo__xiaxing_record")) == "table"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = player:getMark("emo__xiaxing_record")
    room:setPlayerMark(player, "emo__xiaxing_record", 0)
    room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"))
  end,
})

Fk:loadTranslationTable{
  ["emo__xiaxing"] = "侠行",
  [":emo__xiaxing"] = "锁定技，每轮开始时，你从技能描述字数分别为1~30，31~60，61~90的三个技能中选择一个于本轮内获得。",
  ["#emo__xiaxing-skill"] = "侠行：选择一个技能于本轮内获得",
}


return skel
